/*
 * ConsNoticias.java
 *
 * Created on 28 de noviembre de 2008, 15:33
 */
package Vistas;

import Dominio.CtrlDominio;
import Dominio.NoEstaNoticiaException;
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.io.StreamCorruptedException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/**
 *
 * @author  sandra
 */
public class ConsNoticias extends javax.swing.JFrame {
private int idioma;
    private CtrlDominio CD = CtrlDominio.getInstance();
    private Vector noticias = new Vector<String>(0, 1);
    private Vector temas = new Vector<String>();
    private Vector palabras = new Vector<String>(0, 1);
    private static String[] cabecerasTablaTema = new String[]{"Lista de Temas de la noticia"};
    private static String[] cabecerasTablaPalabra = new String[]{"Lista de Palabras de la noticia"};
    private String fs = System.getProperty("file.separator");

    /** Creates new form ConsNoticias */
    public ConsNoticias(int id) throws StreamCorruptedException, ClassNotFoundException, IOException {
//        String pt = "/home/sandra/Escritorio/diccTema.dat";
  //      String pn = "/home/sandra/Escritorio/diccNot.dat";
    //    String pp = "/home/sandra/Escritorio/diccPal.dat";
      //  CD.cargarDiccionarios(pn, pp, pt, pp);
        noticias = CD.listarNoticias();
        if (noticias.size() == 0) {
            JOptionPane.showMessageDialog(this, "No es posible consultar las noticias, porque no hay ninguna noticia en el diccionario", "No hay noticias en el diccionario", JOptionPane.INFORMATION_MESSAGE);
            this.dispose();
            new menu(idioma).setVisible(true);
        } else {
            this.setTitle("Consultar Noticia");
            this.setLocationRelativeTo(null);
            initComponents();
            idioma=id;
             this.setLocationRelativeTo(null);
            for (int tam = 0; tam < noticias.size(); tam++) {
                jComboNoticias.addItem(noticias.get(tam));
            }
             ImageIcon ic = new ImageIcon(System.getProperty("user.dir") + fs + "src" + fs + "Image" + fs + "return.png");
             jBtCerrar.setIcon(ic);
             ImageIcon ic2 = new ImageIcon(System.getProperty("user.dir") + fs + "src" + fs + "Image" + fs + "viewmag.png");
             jButton1.setIcon(ic2);
             jBtVisualizar.setIcon(ic2);
             ImageIcon ic3 = new ImageIcon(System.getProperty("user.dir") + fs + "src" + fs + "Image" + fs + "book_open.png");
            jBtConsNoticia.setIcon(ic3);          
        }
    }

    public ConsNoticias(String noticia, int id) {
        try {
            idioma=id;
              this.setTitle("Consultar Noticia");
            initComponents();
             this.setLocationRelativeTo(null);
            jComboNoticias.addItem(noticia);
            
            palabras = CD.listarPalabrasNoticia(noticia);
            Object[][] aux2 = new Object[palabras.size()][1];
            for (int i = 0; i < palabras.size(); i++) {
                aux2[i][0] = palabras.get(i);
            }
            javax.swing.table.DefaultTableModel pp = new javax.swing.table.DefaultTableModel(aux2, cabecerasTablaPalabra);
            jPalabras.setModel(pp);
            jSeccion.setText(CD.getSeccionNoticia(noticia));
            temas = CD.listarTemaNoticia(noticia);
            Object[][] aux = new Object[temas.size()][1];
            for (int i = 0; i < temas.size(); i++) {
                aux[i][0] = temas.get(i);
            }
            
            jTemas.setModel(new javax.swing.table.DefaultTableModel(aux, cabecerasTablaTema));

           
        } catch (NoEstaNoticiaException ex) {
            Logger.getLogger(ConsNoticias.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jComboNoticias = new javax.swing.JComboBox();
        jBtConsNoticia = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jSeccion = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTemas = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        jPalabras = new javax.swing.JTable();
        jBtCerrar = new javax.swing.JButton();
        jBtVisualizar = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Noticia: ");

        jComboNoticias.setEditable(true);

        jBtConsNoticia.setText("Consultar noticia");
        jBtConsNoticia.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jBtConsNoticiaMouseClicked(evt);
            }
        });

        jLabel2.setText("Seccion:");

        jSeccion.setText(" ");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addComponent(jSeccion)
                        .addContainerGap())
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboNoticias, 0, 367, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jBtConsNoticia)
                        .addGap(12, 12, 12))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBtConsNoticia)
                    .addComponent(jComboNoticias, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jSeccion))
                .addContainerGap(2, Short.MAX_VALUE))
        );

        jTemas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null},
                {null},
                {null},
                {null}
            },
            new String [] {
                "Lista de Temas de la noticia"
            }
        ));
        jTemas.setCellSelectionEnabled(true);
        jTemas.setEditingRow(0);
        jTemas.setName("ejemplo\n"); // NOI18N
        jTemas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jTemasMouseEntered(evt);
            }
        });
        jScrollPane1.setViewportView(jTemas);

        jPalabras.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null},
                {null},
                {null},
                {null}
            },
            new String [] {
                "Lista de Palabras de la noticia"
            }
        ));
        jScrollPane2.setViewportView(jPalabras);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 295, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 285, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE)
        );

        jBtCerrar.setText("Volver");
        jBtCerrar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jBtCerrarMouseClicked(evt);
            }
        });

        jBtVisualizar.setText("Ver Noticia Origen");
        jBtVisualizar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jBtVisualizarMouseClicked(evt);
            }
        });

        jButton1.setText("Ver Noticia Clasificada");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 244, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(jBtVisualizar, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(21, 21, 21)
                        .addComponent(jBtCerrar, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jBtCerrar)
                    .addComponent(jBtVisualizar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void jBtConsNoticiaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jBtConsNoticiaMouseClicked
        try {
            String noticia = (String) jComboNoticias.getSelectedItem();
            temas = CD.listarTemaNoticia(noticia);
            Object[][] aux = new Object[temas.size()][1];
            for (int i = 0; i < temas.size(); i++) {
                aux[i][0] = temas.get(i);
            }
            jTemas.setModel(new javax.swing.table.DefaultTableModel(aux, cabecerasTablaTema));
            jSeccion.setText(CD.getSeccionNoticia(noticia));
            palabras = CD.listarPalabrasNoticia(noticia);
            Object[][] aux2 = new Object[palabras.size()][1];
            for (int i = 0; i < palabras.size(); i++) {
                aux2[i][0] = palabras.get(i);
            }
            jPalabras.setModel(new javax.swing.table.DefaultTableModel(aux2, cabecerasTablaPalabra));
        } catch (NoEstaNoticiaException ex) {
            Logger.getLogger(ConsNoticias.class.getName()).log(Level.SEVERE, null, ex);
        }
    /*String[] tem = new String[temas.size()];
    for (int tam = 0; tam < temas.size(); tam++) {
    tem[tam] = noticias.get(tam);
    } // TODO add your handling code here:
    Vector nombreColumnas = new Vector();
    String x = new String("Temas de la noticia");
    nombreColumnas.add(x);
    DefaultTableModel datos = new DefaultTableModel(temas, nombreColumnas);
    jTemas.setModel(datos);
    /*
    Vector nombreColumnas2 = new Vector();
    String aux = new String("Palabras de la noticia");
    nombreColumnas2.add(aux);
    DefaultTableModel datos2 = new DefaultTableModel(temas, nombreColumnas2);
    jTemas.setModel(datos2);*/
    /*} catch (NoEstaNoticiaException ex) {
    Logger.getLogger(ConsNoticias.class.getName()).log(Level.SEVERE, null, ex);
    }*/

    }//GEN-LAST:event_jBtConsNoticiaMouseClicked

    private void jBtCerrarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jBtCerrarMouseClicked
        this.dispose();
        new menu(idioma).setVisible(true);
    }//GEN-LAST:event_jBtCerrarMouseClicked

    private void jBtVisualizarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jBtVisualizarMouseClicked
        try {
            String noticia = (String) jComboNoticias.getSelectedItem();
            File path = new File(CD.getPath(noticia));
            Desktop.getDesktop().open(path);
            //new VerNoticia(noticia).setVisible(true);
        } catch (NoEstaNoticiaException ex) {                                          
            JOptionPane.showMessageDialog(this, "La noticia que desea visualizar no se encuentra en el diccionario", "Error: no se encuentra la noticia", JOptionPane.ERROR_MESSAGE);
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(this,"No se puede visualizar la noticia ","Error de de lectura", JOptionPane.ERROR_MESSAGE);
        }catch(IllegalArgumentException ex){
            JOptionPane.showMessageDialog(this, "No se puede visualizar la noticia porque se ha movido o borrado", "Error: no se puede visualizar la noticia", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jBtVisualizarMouseClicked

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
        try {
            String noticia = (String) jComboNoticias.getSelectedItem();
            File path = new File(System.getProperty("user.dir") + fs + "src" + fs + "NoticiasXML" + fs + noticia + ".xml");
            Desktop.getDesktop().open(path);
            //new VerNoticia(noticia).setVisible(true);
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(this,"No se puede visualizar la noticia ","Error de de lectura", JOptionPane.ERROR_MESSAGE);
        }catch(IllegalArgumentException ex){
            JOptionPane.showMessageDialog(this, "No se puede visualizar la noticia porque se ha movido o borrado", "Error: no se puede visualizar la noticia", JOptionPane.ERROR_MESSAGE);
        }// TODO add your handling code here:
    }//GEN-LAST:event_jButton1MouseClicked

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jTemasMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTemasMouseEntered
        jTemas.setToolTipText((String) jComboNoticias.getSelectedItem());        // TODO add your handling code here:
    }//GEN-LAST:event_jTemasMouseEntered

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBtCerrar;
    private javax.swing.JButton jBtConsNoticia;
    private javax.swing.JButton jBtVisualizar;
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboNoticias;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTable jPalabras;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel jSeccion;
    private javax.swing.JTable jTemas;
    // End of variables declaration//GEN-END:variables
}
